package br.com.javamagazine.leilaojm.ejb;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.javamagazine.leilaojm.domain.*;

@Stateless
public class AvaliacaoService {

	@PersistenceContext
	private EntityManager entityManager;

	public void salvaAvaliacao(Avaliacao avaliacao) {
		entityManager.persist(avaliacao);
	}

	public void excluiAvaliacao(Avaliacao avaliacao) {
		// evita erro de entidade nao gerenciada pelo contexto
		avaliacao = entityManager.find(Avaliacao.class, avaliacao.getId());
		entityManager.remove(avaliacao);
	}

	@SuppressWarnings("unchecked")
	public List<Avaliacao> listaAvaliacoesDoModelo(Integer idModelo) {
		Query query = entityManager
				.createQuery("Select a from Avaliacao a where a.modelo.id = :idModelo");
		query.setParameter("idModelo", idModelo);
		return query.getResultList();

	}
}
